// 引入axios
import axios from 'axios'
// 请求延时（毫秒数，如果请求话费超过了'timeout'的时间，请求将被中断）
axios.defaults.timeout = 100000

const api_name = 'http://localhost:8333/user/userInfo'

// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
    // 在请求发送之前，将 token 放入请求头中
    const token = localStorage.getItem('authorization');
    if (token) {
      config.headers.Authorization = `${token}`;
    }
    return config;
  }, function (error) {
    return Promise.reject(error);
  });

export default {

    getAllUser() {
        return axios.get(`${api_name}/` + "getAllUser")
    },

    //更改当前用户的信息
    updateNowUser(editUser) {
        return axios.put(`${api_name}/` + "updateNowUser",{
            hhid : editUser.hhid,
            nickName : editUser.nickName,
            sex : editUser.sex,
            email: editUser.email,
            brief : editUser.brief,
        })
    },
    //获取当前登录用户信息
    getNowUserWithCredit() {
        return axios.get(`${api_name}/` + "getNowUserWithCredit")
    },
    //退出
    logout() {
        return axios.post(`${api_name}/` + "logout")
    },
    //获取登录用户
    getLoginUser() {
        return axios.get(`${api_name}/` + "getLoginUser")
    },
	//用户列表
    pageList(page, limit, searchObj) {
        return axios.post(`${api_name}/${page}/${limit}`,{
            hhid : searchObj.hhid,
            name : searchObj.name,
            sex : searchObj.sex,
            email : searchObj.email,
            limits : searchObj.limits
        })
    },
    //删除某一个用户
    remove(id) {
        return axios.delete(`${api_name}/` + "remove" + `/${id}`)
    },
    //跟据id查找某一个用户
    getUserById(id) {
        return axios.get(`${api_name}/` + "get" + `/${id}`)
    },
    //修改用户
    updateUser(editUser) {
        return axios.put(`${api_name}/` + "update",{
            id : editUser.id,
            avatar : editUser.avatar,
            hhid : editUser.hhid,
            nickName : editUser.nickName,
            sex : editUser.sex,
            email: editUser.email,
            password : editUser.password,
            brief : editUser.brief,
            limits : editUser.limits
        })
    },

    //发送验证码
    sendMail(mail) {
        return axios.post(`${api_name}/` + "sendMail" + `/${mail}`)
    },

    //登录
    login(mail, code) {
        return axios.post(`${api_name}/` + "loginVerification", {
            email : mail,
            verificationCode: code
        })
    }
}